package com.meixiang.adapter.personalCenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.activity.account.myShopper.OrderSaleActivity;
import com.meixiang.activity.homes.shopping.GoodsDetailActivity;
import com.meixiang.entity.myOrder.MyOrderDetail;
import com.meixiang.entity.myOrder.OrderUtils;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.GlideHelper;

import butterknife.Bind;
import butterknife.ButterKnife;


public class AllOrderItemAdapter extends RecyclerView.Adapter<AllOrderItemAdapter.ViewHolder> {

    private Activity context;
    private int position;
    private MyOrderDetail orderDetail;
//    //待收货，交易完成，待评价：显示（退款售后） 其余不显示
//    private String status,refundState; //20，30、40状态，且refundState为空，显示 退款售后

    private int type;//0其他，1显示（退换售后）按钮,int type
//
    public void setType(int type) {
        this.type = type;
    }


    public AllOrderItemAdapter(Activity context) {
        this.context = context;
//        this.type=type;
    }

    public AllOrderItemAdapter(Activity context, MyOrderDetail orderDetail) {
        this.context = context;
        this.position = orderDetail.getOrderGoodsList().size();
        this.orderDetail=orderDetail;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_all_order_custom_list_items, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if(this.position-1!=position){
            holder.viewLine.setVisibility(View.VISIBLE);
        }else{
            holder.viewLine.setVisibility(View.GONE);
        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);

                }
            });
        }

        //20，30、40状态，且refundState为空，显示 退款售后
//        if(type==1){
//            if("待收货".equals(status)||"待评价".equals(status)||"交易完成".equals(status)){
//                holder.tvAfterSale.setVisibility(View.VISIBLE);
//            }else{
//                holder.tvAfterSale.setVisibility(View.GONE);
//            }
//        }else{
//            holder.tvAfterSale.setVisibility(View.GONE);
//        }
        if(type==1){
                //0不显示，1显示按钮，2显示提示文字
                int type = OrderUtils.isShowRefund(orderDetail.getOrderState(), orderDetail.getOrderGoodsList().get(position).getRefundState());
                if(type==1){
                    holder.tvAfterSale.setVisibility(View.VISIBLE);
                    holder.tvAfterSale.setBackgroundResource(R.drawable.all_order_item_border_gray);
                }else if(type==2){
                    holder.tvAfterSale.setVisibility(View.VISIBLE);
                    holder.tvAfterSale.setBackgroundColor(ContextCompat.getColor(context,R.color.order_item_background));
                    String orderStatus = OrderUtils.getOrderStatus(orderDetail.getOrderState(), orderDetail.getEvaluationStatus(), orderDetail.getOrderGoodsList().get(position).getRefundState());
                    holder.tvAfterSale.setTextColor(ContextCompat.getColor(context,R.color.order_red_color));
                    holder.tvAfterSale.setText(orderStatus);
                }else{
                    holder.tvAfterSale.setVisibility(View.GONE);
                }
        }
        if(orderDetail!=null){
            MyOrderDetail.OrderGoodsListBean orderGoodsListBean = orderDetail.getOrderGoodsList().get(position);
            if(orderGoodsListBean.getSpecInfo()!=null){
                holder.tvKeyword.setText(OrderUtils.getSpecInfo(orderGoodsListBean.getSpecInfo().toString()));
                holder.tvAmount.setText("x " +orderGoodsListBean.getGoodsNum());
            }else{
                holder.tvAmount.setText("x 0");
            }
            holder.tvDetail.setText(orderGoodsListBean.getGoodsName());
            holder.tvPrice.setText("￥"+orderGoodsListBean.getGoodsPrice());
            if(!AbStrUtil.isEmpty(orderGoodsListBean.getGoodsImage())){
                GlideHelper.showImage(context, orderGoodsListBean.getGoodsImage(),
                        R.drawable.image_default_gray_bg,holder.allOrderItemIv);
            }
        }

        if(holder.tvAfterSale.getVisibility()==View.VISIBLE){
            holder.tvAfterSale.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    int type = OrderUtils.isShowRefund(orderDetail.getOrderState(), orderDetail.getOrderGoodsList().get(position).getRefundState());
                    if(type==1){
                        intent.setClass(context, OrderSaleActivity.class);
                        intent.putExtra("orderId",orderDetail.getOrderId());
                        intent.putExtra("recId",orderDetail.getOrderGoodsList().get(position).getRecId());
                        intent.putExtra("orderName",orderDetail.getOrderGoodsList().get(position).getGoodsName());
                    }else if(type==2){
                        intent.setClass(context, GoodsDetailActivity.class);
                        if(!AbStrUtil.isEmpty(orderDetail.getOrderGoodsList().get(position).getGoodsId())){
                            intent.putExtra(GoodsDetailActivity.GOODS_ID,orderDetail.getOrderGoodsList().get(position).getGoodsId());
                            context.startActivity(intent);
                        }
                        return;
                    }
                    context.startActivity(intent);
                    context.finish();
                }
            });
        }

    }



    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int positions);
    }

    @Override
    public int getItemCount() {
        return orderDetail.getOrderGoodsList().size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.all_order_item_iv)
        ImageView allOrderItemIv;
        @Bind(R.id.all_order_item_tv_detail)
        TextView tvDetail;
        @Bind(R.id.all_order_item_tv_price)
        TextView tvPrice;
        @Bind(R.id.all_order_item_tv_keyword)
        TextView tvKeyword;
        @Bind(R.id.all_order_item_tv_amount)
        TextView tvAmount;
        @Bind(R.id.all_order_item_tv_after_sale)
        TextView tvAfterSale;
        @Bind(R.id.all_order_item_view_line)
        View viewLine;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
